/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package r2x;

import java.util.LinkedList;


/**
 *
 * @author przemek
 */
public class TabInfo {

    public static int NC = -1;
    public static int STAND_ALONE = 0;
    public static int NNRELATION = 1;
    public static int CENTRAL = 2;


    private String tabName;
    private int columnNumber =0;
    private int refNumber =0;  // number of tables to which refers this table
    private int beingRefNumber =0; // number of tables that refer to this table
    private int type = NC;
    private LinkedList<TabInfo> referToList;
    private LinkedList<TabInfo> referredByList;
    private LinkedList<ColumnInfo> columnsList;
    private LinkedList<String> primKeyNames;

    TabInfo (){
        referToList = new LinkedList<TabInfo>();
        referredByList = new LinkedList<TabInfo>();
        columnsList = new LinkedList<ColumnInfo>();
        primKeyNames = new LinkedList<String>();
    }

    TabInfo(String name) {
        this();
        tabName = ""+name;
    }

    public Boolean isStandAloneTable(){
        if ((refNumber == 0) && (beingRefNumber ==0)){
            return true;
        }
        return false;
    }

    public Boolean isNNRelationTable(){
        if (referToList.size() == columnNumber){
            return true;
        }
        return false;
    }
    /**
     * @return the tabName
     */
    public String getTabName() {
        return tabName;
    }

    /**
     * @param tabName the tabName to set
     */
    public void setTabName(String tabName) {
        this.tabName = tabName;
    }

    /**
     * @return the columnNumber
     */
    public int getColumnNumber() {
        return columnNumber;
    }

    /**
     * @param columnNumber the columnNumber to set
     */
    public void setColumnNumber(int columnNumber) {
        this.columnNumber = columnNumber;
    }

    /**
     * @return the refNumber
     */
    public int getRefNumber() {
        return refNumber;
    }

    /**
     * @param refNumber the refNumber to set
     */
    public void setRefNumber(int refNumber) {
        this.refNumber = refNumber;
    }

    /**
     * @return the fkNumber
     */
    public int getBeingRefNumber() {
        return beingRefNumber;
    }

    /**
     * @param fkNumber the fkNumber to set
     */
    public void setBeingRefNumber(int fkNumber) {
        this.beingRefNumber = fkNumber;
    }

    @Override
    public String toString(){
        String s =  "\n"+tabName+"\ncolumns: "+columnNumber
                    +"   fks: "+refNumber+"   referred: "
                    +beingRefNumber+" times\nrefers to: "
                    + getTabNamesFromTabInfoListAsOneString(referToList)
                    + "referred by: "
                    + getTabNamesFromTabInfoListAsOneString(referredByList)
                    + "Columns: \n"
                    + getColumnsFromColumnListAsOneString(columnsList)
                    + "type: "
                    + type
                    + "\n-------------------\n";

        return s;
    }

    /**
     * @return the referToList
     */
    public LinkedList<TabInfo> getReferToList() {
        return referToList;
    }

    /**
     * @param referToList the referToList to set
     */
    public void setReferToList(LinkedList<TabInfo> referToList) {
        this.referToList = referToList;
    }

    /**
     * @return the referedByList
     */
    public LinkedList<TabInfo> getReferredByList() {
        return referredByList;
    }

    /**
     * @param referedByList the referedByList to set
     */
    public void setReferredByList(LinkedList<TabInfo> referedByList) {
        this.referredByList = referedByList;
    }

    public String getTabNamesFromTabInfoListAsOneString 
                                    (LinkedList <TabInfo> list){
        String resultStr = "";
        for (int i = 0; i<list.size(); ++i){
            resultStr = resultStr + list.get(i).getTabName()+", ";
        }
        resultStr = resultStr + "("+list.size()+")\n";
        return resultStr;
    }

    public String getColumnsFromColumnListAsOneString
                                    (LinkedList <ColumnInfo> list){
        String resultStr = "";
        for (int i = 0; i<list.size(); ++i){
            resultStr = resultStr + list.get(i).toString();
        }
        return resultStr;
    }


    /**
     * @return the columnsList
     */
    public LinkedList<ColumnInfo> getColumnsList() {
        return columnsList;
    }

    /**
     * @param columnsList the columnsList to set
     */
    public void setColumnsList(LinkedList<ColumnInfo> columnsList) {
        this.columnsList = columnsList;
    }

    /**
     * @return the primKeyNames
     */
    public LinkedList<String> getPrimKeyNames() {
        return primKeyNames;
    }

    /**
     * @param primKeyNames the primKeyNames to set
     */
    public void setPrimKeyNames(LinkedList<String> primKeyNames) {
        this.primKeyNames = primKeyNames;
    }

    /**
     * @return the type
     */
    public int getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(int type) {
        this.type = type;
    }

}
